package com.yami.shop.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Ecosphere;
import com.yami.shop.bean.model.EcosphereClassification;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.EcosphereClassificationService;
import com.yami.shop.service.EcosphereService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/ecosphere")
public class EcosphereController {

    @Autowired
    private EcosphereService ecosphereService;
    @Autowired
    private EcosphereClassificationService ecosphereClassificationService;

    @GetMapping("/ecosphereClassification/getAllEcosphereClassification")
    @Operation(summary =  "查询", description = "查询分类")
    public ServerResponseEntity<List<EcosphereClassification>> getNoticePage(EcosphereClassification ecosphereClassification) {
        List<EcosphereClassification> noticePage = ecosphereClassificationService.list(new LambdaQueryWrapper<EcosphereClassification>()
                .like(ObjectUtil.isNotEmpty(ecosphereClassification.getName()),EcosphereClassification::getName, ecosphereClassification.getName())
                .ne(EcosphereClassification::getStatus,1)
                .orderByAsc(EcosphereClassification::getSort).orderByDesc(EcosphereClassification::getCreateTime));
        return ServerResponseEntity.success(noticePage);
    }
    /**
     *
     * @param
     * @param id
     * @return
     */
    @GetMapping("/ecosphereClassification/getOneById")
    @Operation(summary =  "获取详情", description = "获取详情")
    public ServerResponseEntity<EcosphereClassification> getOneById(@RequestParam Long id) {
        EcosphereClassification noticePage = ecosphereClassificationService.getById(id);
        return ServerResponseEntity.success(noticePage);
    }
    /**
     *
     * @param
     * @param ecosphereClassification
     * @return
     */
    @PutMapping("/ecosphereClassification/updateOneById")
    @Operation(summary =  "修改", description = "修改")
    public ServerResponseEntity updateOneById(@RequestBody EcosphereClassification ecosphereClassification) {
        ecosphereClassificationService.updateById(ecosphereClassification);
        return ServerResponseEntity.success();
    }

    /**
     *
     * @param
     * @param id
     * @return
     */
    @DeleteMapping("/ecosphereClassification/deleteOneById/{ids}")
    @Operation(summary =  "删除", description = "删除")
    public ServerResponseEntity deleteOneById(@PathVariable Long[] ids) {
        ecosphereClassificationService.removeBatchByIds(Arrays.asList(ids));
        return ServerResponseEntity.success();
    }

    /**
     *
     * @param
     * @param ecosphereClassification
     * @return
     */
    @PostMapping("/ecosphereClassification/save")
    @Operation(summary =  "保存", description = "保存")
    public ServerResponseEntity deleteOneById(@RequestBody EcosphereClassification ecosphereClassification) {
        ecosphereClassification.setCreateTime(new Date());
        ecosphereClassificationService.save(ecosphereClassification);
        return ServerResponseEntity.success();
    }

    @GetMapping("/pageEcosphere")
    @Operation(summary =  "分页查询", description = "分页查询")
    public ServerResponseEntity<IPage<Ecosphere>> getNoticePage(PageParam<Ecosphere> page, Ecosphere ecosphere) {
        IPage<Ecosphere> noticePage = ecosphereService.page(page, new LambdaQueryWrapper<Ecosphere>()
                .like(ObjectUtil.isNotEmpty(ecosphere.getName()),Ecosphere::getName, ecosphere.getName())
                .ne(Ecosphere::getStatus,1)
                .eq(ObjectUtil.isNotEmpty(ecosphere.getEcosphereCode()),Ecosphere::getEcosphereCode, ecosphere.getEcosphereCode())
                .orderByAsc(Ecosphere::getSort).orderByDesc(Ecosphere::getCreateTime));
        return ServerResponseEntity.success(noticePage);
    }


    /**
     *
     * @param
     * @param id
     * @return
     */
    @GetMapping("/getEcosphereOneById")
    @Operation(summary =  "获取详情", description = "获取详情")
    public ServerResponseEntity<Ecosphere> getEcosphereOneById(@RequestParam Long id) {
        Ecosphere ecosphere = ecosphereService.getById(id);
        return ServerResponseEntity.success(ecosphere);
    }
    /**
     *
     * @param
     * @param ecosphere
     * @return
     */
    @PutMapping("/updateEcosphereOneById")
    @Operation(summary =  "修改", description = "修改")
    public ServerResponseEntity updateEcosphereOneById(@RequestBody Ecosphere ecosphere) {
        ecosphereService.updateById(ecosphere);
        return ServerResponseEntity.success();
    }

    /**
     *
     * @param
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteEcosphereOneById/{ids}")
    @Operation(summary =  "删除", description = "删除")
    public ServerResponseEntity deleteEcosphereOneById(@PathVariable Long[] ids) {
        ecosphereService.removeBatchByIds(Arrays.asList(ids));
        return ServerResponseEntity.success();
    }

    /**
     *
     * @param
     * @param ecosphere
     * @return
     */
    @PostMapping("/saveEcosphere")
    @Operation(summary =  "保存", description = "保存")
    public ServerResponseEntity saveEcosphere(@RequestBody Ecosphere ecosphere) {
        ecosphere.setCreateTime(new Date());
        ecosphereService.save(ecosphere);
        return ServerResponseEntity.success();
    }
}
